package com.tepth.appmanageservice.protocol.packets;

import com.tepth.appmanageservice.protocol.Index;
import com.tepth.appmanageservice.protocol.interfaces.IReceive;
import com.tepth.appmanageservice.protocol.interfaces.ISend;
import com.tepth.appmanageservice.tools.ext.ExtPro;
import com.tepth.appmanageservice.tools.ext.ExtToBytes;
import com.tepth.appmanageservice.tools.ext.ExtToInt;
import com.tepth.appmanageservice.tools.ext.ExtToString;

public class Header implements ISend, IReceive {
	private static short Serial_Number = 0; // ��Ϣ��ˮ��

	private static short getSerialNumber() {
		return Serial_Number == Short.MAX_VALUE ? Serial_Number = 0 : Serial_Number++;
	}

	private short id; // ��ϢId
	private short bodyLength; // ��Ϣ�峤��
	private boolean subcontracting; // �Ƿ�ְ�
	private boolean subcontractor = false;
	private boolean isAES = false;// �Ƿ����
	private short serialNumber; // ��Ϣ��ˮ��
	private short totalNumber; // ��Ϣ�ܰ���
	private short packetSequenceNumber; // ��Ϣ�����
	private byte[] result;

	public short getId() {
		return this.id;
	}

	public void setId(short id) {
		this.id = id;
	}

	public short getBodyLength() {
		return this.bodyLength;
	}

	public void setBodyLength(short bodyLength) {
		this.bodyLength = bodyLength;
	}

	public boolean getIsAES() {
		return this.isAES;
	}

	public void setIsAES(boolean isAES) {
		this.isAES = isAES;
	}

	public short getTotalNumber() {
		return totalNumber;
	}

	public void setTotalNumber(short totalNumber) {
		this.totalNumber = totalNumber;
	}

	public short getPacketSequenceNumber() {
		return packetSequenceNumber;
	}

	public void setPacketSequenceNumber(short packetSequenceNumber) {
		this.packetSequenceNumber = packetSequenceNumber;
	}

	public Header() {

	}

	public Header(short id, short bodyLength) {
		this.id = id;
		this.bodyLength = bodyLength;
	}

	public boolean isSubcontractor() {
		return this.subcontracting;
	}

	public void isSubcontractor(boolean subcontracting) {
		this.subcontracting = subcontracting;
	}

	public boolean isSubcontractor1() {
		return subcontractor;
	}

	public void isSubcontractor1(boolean subcontractor) {
		this.subcontractor = subcontractor;
	}

	public short getSerialNumber1() {
		return this.serialNumber;
	}

	@Override
	public byte[] getResult() {
		return this.result;
	}

	@Override
	public void setLength(int length) {

	}

	@Override
	public short getLength() {
		return (short) this.result.length;
	}

	@Override
	public void Receive(byte[] data, Index index) {
		this.setId(ExtToInt.fromByteArrayTo16(data, index));

		byte[] bodyProperty_byte = ExtToBytes.fromByteArray(data, index, 2);
		String bodyProperty = ExtToString.fromByteArray(bodyProperty_byte);

		this.setBodyLength(Short.valueOf(bodyProperty.substring(2), 2));

		this.serialNumber = ExtToInt.fromByteArrayTo16(data, index);

		char[] charbodyProperty = bodyProperty.toCharArray();
		if (charbodyProperty[0] == '1') {
			this.setTotalNumber(ExtToInt.fromByteArrayTo16(data, index));
			this.setPacketSequenceNumber(ExtToInt.fromByteArrayTo16(data, index));

			this.subcontracting = true;
		} else {
			this.subcontracting = false;
		}

		this.setIsAES(charbodyProperty[1] == '1');
	}

	@Override
	public void load() {
		byte[] id_btyes = ExtToBytes.fromNumber(this.getId());
		byte[] body_length_btyes_r = ExtToBytes.fromNumber(this.getBodyLength());
		byte[] body_length_btyes = new byte[] { body_length_btyes_r[1], body_length_btyes_r[0] };
		this.serialNumber = getSerialNumber();
		byte[] serialNumber_btyes = ExtToBytes.fromNumber(this.serialNumber);

		if (ExtPro.getClient().getAES_KEYS() != null) {
			String body_length_2 = ExtToString.fromByteArray(body_length_btyes);
			String body_length_2_01 = "01" + body_length_2.substring(2, 8);
			String body_length_2_02 = body_length_2.substring(8);
			byte byte01 = ExtToBytes.fromStringBit(body_length_2_01);
			byte byte02 = ExtToBytes.fromStringBit(body_length_2_02);
			body_length_btyes = new byte[] { byte01, byte02 };
		}
		this.result = ExtToBytes.fromByteArray2(new byte[][] { id_btyes, body_length_btyes, serialNumber_btyes });
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("��ϢID��");
		sb.append(Integer.toHexString(this.getId()));
		sb.append(" ��ˮ�ţ�");
		sb.append(this.getSerialNumber1());
		sb.append(" �Ƿ�ְ���");
		sb.append(this.isSubcontractor());

		if (isSubcontractor()) {
			sb.append(" �ܰ�����");
			sb.append(this.getTotalNumber());
			sb.append(" ����ţ�");
			sb.append(this.getPacketSequenceNumber());
		}

		sb.append(" ��Ϣ�峤�ȣ�");
		sb.append(this.getBodyLength());

		return sb.toString();
	}
}